package com.kevin.actoviti.day01;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.apache.commons.io.IOUtils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Classname QueryBpmnFile
 * @Description TODO
 * @Date 2020/6/22 0:34
 * @Created by kevin
 */
public class QueryBpmnFile {

    public static void main(String[] args) throws IOException {

        //获取processEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取repositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //得到查询其对象ProcessDefinitionQuery
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        //设置查询条件，查询指定的信息
        processDefinitionQuery.processDefinitionKey("holiday");
        ProcessDefinition processDefinition = processDefinitionQuery.singleResult();
        //获取部署信息
        String deploymentId = processDefinition.getDeploymentId();
        //资源repositoryService获取文件的输入流
        InputStream pngIs = repositoryService.getResourceAsStream(deploymentId, processDefinition.getDiagramResourceName());
        InputStream bpmnIs = repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName());

        //构建输出流
        FileOutputStream pngOs = new FileOutputStream("C:\\软件\\activiti\\资料" + processDefinition.getDiagramResourceName());
        FileOutputStream bpmnOs = new FileOutputStream("C:\\软件\\activiti\\资料" + processDefinition.getResourceName());
        //输入、输出流的转换
        IOUtils.copy(pngIs, pngOs);
        IOUtils.copy(bpmnIs, bpmnOs);
        bpmnOs.close();
        pngOs.close();
        bpmnIs.close();
        pngIs.close();

    }
}
